package com.kmproject.haifu.mvp.ui.holder;

import android.app.Activity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.kmproject.haifu.R;
import com.kmproject.haifu.mvp.model.entity.beans.HousePayWayBean;
import com.kmproject.haifu.mvp.ui.adapter.PopuWindowSelectSxItemNewAdapter;
import com.kmproject.haifu.mvp.ui.adapter.PopuWindowSelectSxNewAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SxSelectHolderNew {
    @BindView(R.id.popuwindows_seelct_sx_new_RecyclerView1)
    RecyclerView popuwindowsSeelctSxNewRecyclerView1;
    @BindView(R.id.popuwindows_seelct_sx_clearTv)
    TextView popuwindowsSeelctSxClearTv;
    @BindView(R.id.popuwindows_seelct_sx_sureTv)
    TextView popuwindowsSeelctSxSureTv;
    PopuWindowSelectSxNewAdapter selectSxNewAdapter;
    int temDefultIndex = 0;
    List<HousePayWayBean> mhouseInfoTitleBeans;
    selectCallBack mselectCallBack;
    @BindView(R.id.popuwindows_seelct_sx_new_RecyclerView_foot)
    RecyclerView popuwindowsSeelctSxNewRecyclerViewFoot;

    public SxSelectHolderNew(Activity context, View view, selectCallBack selectCallBack) {
//        R.layout.popuwindows_seelct_sx_new
        ButterKnife.bind(this, view);
        mselectCallBack = selectCallBack;
    }

    /**
     * 初始化
     *
     * @param housePayWayBean
     */
    public void initShow(Activity context, HousePayWayBean housePayWayBean, List<HousePayWayBean> houseInfoTitleBeans) {
        mhouseInfoTitleBeans = houseInfoTitleBeans;
        temDefultIndex = getDefaultIndex(housePayWayBean, houseInfoTitleBeans);
        selectSxNewAdapter = new PopuWindowSelectSxNewAdapter(new selectCallBack() {
            @Override
            public void callBack(HousePayWayBean housePayWayBean) {
                //选择短租的房屋类型的时候，显示第三级
              if (houseInfoTitleBeans==null)
              {
                  popuwindowsSeelctSxNewRecyclerViewFoot.setVisibility(View.GONE);
              }else {
                  if (housePayWayBean!=null&&housePayWayBean.getChildren()!=null&&!housePayWayBean.getChildren().isEmpty())
                  {
                      popuwindowsSeelctSxNewRecyclerViewFoot.setVisibility(View.VISIBLE);
                      popuwindowsSeelctSxNewRecyclerViewFoot.setLayoutManager(new LinearLayoutManager(context));
                      popuwindowsSeelctSxNewRecyclerViewFoot.setAdapter(new PopuWindowSelectSxNewAdapter(new selectCallBack() {
                          @Override
                          public void callBack(HousePayWayBean housePayWayBean) {

                          }
                      }, housePayWayBean.getChildren()));
                  }
              }
            }
        }, temDefultIndex==-1?new ArrayList<>():houseInfoTitleBeans.get(temDefultIndex).getChildren());
        popuwindowsSeelctSxNewRecyclerView1.setAdapter(selectSxNewAdapter);
        popuwindowsSeelctSxNewRecyclerView1.setLayoutManager(new LinearLayoutManager(context));
        View headerView = LayoutInflater.from(context).inflate(R.layout.popuwindows_seelct_sx_new_header, null);
        RecyclerView popuwindows_seelct_sx_new_RecyclerView1 = headerView.findViewById(R.id.popuwindows_seelct_sx_new_RecyclerView1);
        popuwindows_seelct_sx_new_RecyclerView1.setLayoutManager(new GridLayoutManager(context, 4));
        PopuWindowSelectSxItemNewAdapter popuWindowSelectSxItemNewAdapter = new PopuWindowSelectSxItemNewAdapter(housePayWayBean.getCode(), houseInfoTitleBeans, new PopuWindowSelectSxItemNewAdapter.ItemClickCallBack() {
            @Override
            public void callback(int position, HousePayWayBean housePayWayBean) {
                temDefultIndex = position;
                if (housePayWayBean.getCategory().equals("dz")) {
                    //短租单独处理
                    selectSxNewAdapter.setNewData(housePayWayBean.getChildren());
                    for (HousePayWayBean temHousePayWayBean1 : housePayWayBean.getChildren()) {
                        if (temHousePayWayBean1.getName().equals("房屋类型")) {
//                            selectSxNewAdapter.addData(temHousePayWayBean1.getChildren().get(0).getChildren());
                            popuwindowsSeelctSxNewRecyclerViewFoot.setVisibility(View.VISIBLE);
                            popuwindowsSeelctSxNewRecyclerViewFoot.setLayoutManager(new LinearLayoutManager(context));
                            popuwindowsSeelctSxNewRecyclerViewFoot.setAdapter(new PopuWindowSelectSxNewAdapter(new selectCallBack() {
                                @Override
                                public void callBack(HousePayWayBean housePayWayBean) {

                                }
                            }, temHousePayWayBean1.getChildren().get(0).getChildren()));
                            break;
                        }
                    }
                } else {
                    popuwindowsSeelctSxNewRecyclerViewFoot.setVisibility(View.GONE);
                    selectSxNewAdapter.setNewData(housePayWayBean.getChildren());
                }

            }
        });
        popuWindowSelectSxItemNewAdapter.setSelectDefaultIndex(temDefultIndex);
        popuwindows_seelct_sx_new_RecyclerView1.setAdapter(popuWindowSelectSxItemNewAdapter);
        selectSxNewAdapter.addHeaderView(headerView);
    }


    @OnClick({R.id.popuwindows_seelct_sx_new_RecyclerView1, R.id.popuwindows_seelct_sx_clearTv, R.id.popuwindows_seelct_sx_sureTv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.popuwindows_seelct_sx_clearTv:
                clearSelect(mhouseInfoTitleBeans);
                mselectCallBack.callBack(null);
                break;
            case R.id.popuwindows_seelct_sx_sureTv:
                mselectCallBack.callBack(mhouseInfoTitleBeans.get(temDefultIndex));
                break;
        }
    }

    public void clearSelect(List<HousePayWayBean> housePayWayBeans){
        if (housePayWayBeans!=null&&!housePayWayBeans.isEmpty())
        {
            for (HousePayWayBean housePayWayBean:housePayWayBeans)
            {
                clearSelectChild(housePayWayBean);
            }
        }
    }

    public void clearSelectChild(HousePayWayBean housePayWayBean)
    {
        housePayWayBean.setSelect(false);
        if (housePayWayBean.getChildren()!=null&&!housePayWayBean.getChildren().isEmpty())
        {
            for (HousePayWayBean housePayWayBean1:housePayWayBean.getChildren())
            {
                housePayWayBean1.setSelect(false);
                clearSelectChild(housePayWayBean1);
            }

        }
    }

    /*
    得到选中哪个 返回-1表示都不选中
     */
    public int getDefaultIndex(HousePayWayBean housePayWayBean, List<HousePayWayBean> houseInfoTitleBeans) {
        if (housePayWayBean == null || houseInfoTitleBeans == null || houseInfoTitleBeans.isEmpty()) {
            return -1;
        }
        for (int i = 0; i < houseInfoTitleBeans.size(); i++) {
            if (housePayWayBean.getCategory().equals(houseInfoTitleBeans.get(i).getCategory())) {
                return i;
            }
        }
        return -1;
    }

    public interface selectCallBack {
        void callBack(HousePayWayBean housePayWayBean);
    }
}
